﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using WorkoutDictionary.Model.Tables;

namespace WorkoutDictionary.View
{
    public partial class ExerciseAdd
    {
        private Guid workoutID;

        private List<string> statuses;

        public ExerciseAdd()
        {
            InitializeComponent();
        }

        private void PhoneApplicationPageLoaded(object sender, RoutedEventArgs e)
        {
            this.statuses = new List<string>
                                {
                                    "Hard",
                                    "Normal",
                                    "Easy"
                                };
        }

        private void ApplicationBarIconButtonClick(object sender, System.EventArgs e)
        {
            string name = this.AutocompleteName.Text;
            string weights = this.TextBoxWeights.Text;
            if(string.IsNullOrWhiteSpace(name) ||
               string.IsNullOrWhiteSpace(weights))
            {
                MessageBox.Show("Enter some information about the exercise");
                return;
            }

            bool hard = this.RadioButtonStatusHard.IsChecked ?? false;
            bool normal = this.RadioButtonStatusNormal.IsChecked ?? false;

            int status = hard ? 3 : (normal ? 2 : 1);
            ((App) Application.Current).StorageManager.CreateAnExercise(name, weights, status, this.workoutID);
            if(NavigationService.CanGoBack)
            {
                NavigationService.GoBack();
            }
            else
            {
                MessageBox.Show("Something wrong happens");
            }
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            var parameters = NavigationContext.QueryString;
            if (parameters.ContainsKey("workoutID"))
            {
                Guid workoutID;
                if (!Guid.TryParse(parameters["workoutID"], out workoutID))
                {
                    MessageBox.Show("So strange error here!");
                    NavigationService.GoBack();
                    return;
                }
                this.workoutID = workoutID;
            }
            else
            {
                MessageBox.Show("Another strange error here!");
                NavigationService.GoBack();
            }
        }
    }
}